//
//  DDShareViewController.m
//  dingdang
//
//  Created by Chen Jing on 15/8/4.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDShareViewController.h"
#import "WeiboSDK.h"
#import "WXApiObject.h"
#define kRedirectURI    @"https://api.weibo.com/oauth2/default.html"
#define kBody @"http://www.dd5s.com"//专业的求解答疑应用，在线解答过程，思路真人语音讲解，让你学会学习。

@interface DDShareViewController ()

@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;
@property(nonatomic,strong)UIImage * snapshotImage;
@property(nonatomic,assign)UIViewController * tempVc;
@property(nonatomic,strong)UIView * shadowView;

@end

@implementation DDShareViewController

+(void)showShareWithController:(UIViewController *)vc{
    DDShareViewController * shareVC = [[DDShareViewController alloc]init];
    shareVC.tempVc = vc;
    
    shareVC.shadowView = [[UIView alloc]initWithFrame:vc.view.bounds];
    shareVC.shadowView.backgroundColor = [UIColor blackColor];
    shareVC.shadowView.alpha = .3;
    [vc.view addSubview:shareVC.shadowView];
    
    
    shareVC.view.frame = CGRectMake(0,CGRectGetHeight(vc.view.bounds) , CGRectGetWidth([[UIScreen mainScreen] bounds]), 220);
    
    [vc addChildViewController:shareVC];
    [vc.view addSubview:shareVC.view];

    [shareVC didMoveToParentViewController:vc];
    UIGraphicsBeginImageContextWithOptions(vc.view.bounds.size, NO, [UIScreen mainScreen].scale);
    [vc.view drawViewHierarchyInRect:vc.view.bounds afterScreenUpdates:YES];
    shareVC.snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [shareVC showShare];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.cancelBtn.layer.cornerRadius = 5;
    self.cancelBtn.layer.masksToBounds = YES;
    self.cancelBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.cancelBtn.layer.borderWidth = .5;
    
    
    //是否安装了qq
    _qqLoginBtn.hidden = ![QQApiInterface isQQInstalled];
    //是否安装了微博
//    _weiboLoginBtn.hidden = ![WeiboSDK isWeiboAppInstalled];
    //是否安装了微信
    _weixinLoginBtn.hidden = ![WXApi isWXAppInstalled];
}

-(void)showShare{
    
    [UIView animateWithDuration:.35 animations:^{
        self.view.frame = CGRectMake(0, CGRectGetHeight(self.tempVc.view.bounds) - 220, CGRectGetWidth([[UIScreen mainScreen] bounds]), 220);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)btn_sinaWeibo:(id)sender {
    
    [self cancel:nil];

    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
    authRequest.redirectURI = kRedirectURI;
    authRequest.scope = @"all";

    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare] authInfo:authRequest access_token:WEIXIN_AppID];
    request.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    //    request.shouldOpenWeiboAppInstallPageIfNotInstalled = NO;
    [WeiboSDK sendRequest:request];
}
- (IBAction)qq:(id)sender {
    
    [self cancel:nil];

    TencentOAuth *tencentOAuth = [[TencentOAuth alloc] initWithAppId:QQ_AppId andDelegate:self];
    QQApiTextObject* txtObj = [QQApiTextObject objectWithText:kBody];
    
    SendMessageToQQReq* req = [SendMessageToQQReq reqWithContent:txtObj];

    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    [self handleSendResult:sent];
}

- (IBAction)weixin:(id)sender {
    
    [self cancel:nil];
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.text = kBody;
    req.bText = YES;
//    req.scene = WXSceneTimeline;
    req.scene = WXSceneSession;

    [WXApi sendReq:req];
}
- (IBAction)cancel:(id)sender {
    [UIView animateWithDuration:.35 animations:^{
        self.view.frame = CGRectMake(0, CGRectGetHeight([[UIScreen mainScreen] bounds]), CGRectGetWidth([[UIScreen mainScreen] bounds]), 220);
    }];
    [self.shadowView removeFromSuperview];
}



- (void)handleSendResult:(QQApiSendResultCode)sendResult
{
    switch (sendResult)
    {
        case EQQAPIAPPNOTREGISTED:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"App未注册" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];

            break;
        }
        case EQQAPIMESSAGECONTENTINVALID:
        case EQQAPIMESSAGECONTENTNULL:
        case EQQAPIMESSAGETYPEINVALID:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送参数错误" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];

            break;
        }
        case EQQAPIQQNOTINSTALLED:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"未安装手Q" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];

            break;
        }
        case EQQAPIQQNOTSUPPORTAPI:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"API接口不支持" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];

            break;
        }
        case EQQAPISENDFAILD:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送失败" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];

            break;
        }
        default:
        {
            break;
        }
    }
}


#pragma mark -
#pragma Internal Method

- (WBMessageObject *)messageToShare{
    WBMessageObject *message = [WBMessageObject message];
    WBImageObject * tempImageObj = [WBImageObject object];
    tempImageObj.imageData = UIImagePNGRepresentation(self.snapshotImage);
    message.text = kBody;
    message.imageObject = tempImageObj;
    return message;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
